package com.zhengb.algorithmdemo.sort;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 对A商品进行秒杀
 *
 * @author zhengbo
 * @date 2020/2/17
 */
public class SecKillShop {

    private final static ReentrantLock GOOD_LOCK = new ReentrantLock();

    /**
     * 回滚流程
     */
    private static void rollBack() {
        System.out.println("执行回滚....");
    }


    /**
     * 购买流程
     */
    private static void doBuy() {
        System.out.println("开始购买");
    }

    public static void main(String[] args) {

        //这里给出抢购商品时的伪代码
        boolean getSuccess = false;
        try {
            getSuccess = GOOD_LOCK.tryLock(2000, TimeUnit.MILLISECONDS);

            if (getSuccess) {
                //执行购买过程
                doBuy();
            }
        } catch (InterruptedException e) {
            rollBack();
        } finally {
            if (!getSuccess) {
                rollBack();
            }
        }
    }
}
